// JavaScript Document
//基础数据
var navData = {
	//桌面
	desktop:[
		{
			id:'d001',
			name:'新增会员',
			icon:'Assets/Images/Icon/memberAdd.png',
			href:'memberAdd.html',
			class:'',
			width:'900px',
			height:'500px'
		},
		{
			id:'d002',
			name:'会员列表',
			icon:'Assets/Images/Icon/memberList.png',
			href:'memberList.html',
			class:'',
			width:'900px',
			height:'500px'
		},
		{
			id:'d003',
			name:'新增应用',
			icon:'Assets/Images/add_icon.png',
			href:'addNew.html',
			class:''
		},
		{
			id:'d004',
			name:'用户列表',
			icon:'Assets/Images/Icon/memberList.png',
			href:'yonghuList.html',
			class:'',
			width:'900px',
			height:'500px'
		},
		{
			id:'d005',
			name:'角色列表',
			icon:'Assets/Images/Icon/memberList.png',
			href:'quanxian.html',
			class:'',
			width:'700px',
			height:'400px'
		},
		{
			id:'d006',
			name:'菜单设置',
			icon:'Assets/Images/Icon/memberList.png',
			href:'menuSet.html',
			class:'',
			width:'600px',
			height:'400px'
		},
		{
			id:'d007',
			name:'消费收银',
			icon:'Assets/Images/Icon/memberList.png',
			href:'xiaofeiList.html',
			class:'',
			width:'900px',
			height:'500px'
		}			
	],
	
	//主导航条
	navbar:[
		/*{
			id:'n001',
			name:'主页',
			icon:'Assets/Images/Icon/home.png',
			href:'home.html',
			class:''
		},
		{
			id:'n002',
			name:'消息',
			icon:'Assets/Images/Icon/chat.png',
			href:'chat.html',
			class:''
		},*/
		{
			id:'n003',
			name:'数据统计',
			icon:'Assets/Images/Icon/char.png',
			href:'char.html',
			class:''
		},
		{
			id:'n004',
			name:'退出登录',
			icon:'{$NOW_THEME_URL}Assets/Images/Icon/loginout.png',
			href:'hdmin.php?s=/Public/logout',
			class:''
		}
	]
};

var allNav = {
		'd001' : {
			name:'新增会员',
			icon:'Assets/Images/Icon/memberAdd.png',
			href:'memberAdd.html',
			class:''
		},
		'd002' : {
			name:'会员列表',
			icon:'Assets/Images/Icon/memberList.png',
			href:'memberList.html',
			class:''
		},
		'd003' : {
			name:'新增应用',
			icon:'Assets/Images/add_icon.png',
			href:'addNew.html',
			class:''
		},
		'd003' : {
			name:'新增应用',
			icon:'Assets/Images/add_icon.png',
			href:'addNew.html',
			class:''
		},
		'n001' : {
			name:'主页',
			icon:'Assets/Images/Icon/home.png',
			href:'home.html',
			class:''
		},
		'n002' : {
			name:'消息',
			icon:'Assets/Images/Icon/chat.png',
			href:'chat.html',
			class:''
		},
		'n003' : {
			name:'数据统计',
			icon:'Assets/Images/Icon/char.png',
			href:'char.html',
			class:''
		},
		'n004' : {
			name:'系统设置',
			icon:'Assets/Images/Icon/settings.png',
			href:'settings.html',
			class:''
		},
	};

//业务逻辑
var openWinList = new Array();
$(function () {
	var themebg_url = 'url(Assets/Images/Deskbg/Windows/desk.jpg)';
	var themebg_url = 'url(Assets/Images/Deskbg/Myself/66.jpg)';
	$('#themebg').css('background-image',themebg_url);
	
	//桌面图标初始化
	function iconinit(){
		//桌面图标初始化
		var  dhtml = '';
		$.each( navData.desktop , function(i, n){
			dhtml += '<li id="'+n.id+'" etitle="'+n.name+'" class="openwin '+n.class+'" ehref="'+n.href+'" ';
			if(n.width){
				dhtml += 'ewidth="'+n.width+'"';
			}
			if(n.height){
				dhtml += 'eheight="'+n.height+'"';
			}
			dhtml += '>';
			dhtml += '<span class="icon"><img src="'+n.icon+'"/></span>';
			dhtml += '<span class="text">'+n.name+'</span>';
			dhtml += '</li>';
		});
		$('#desktop>ul').html(dhtml);
		
		//主导航图标初始化
		var  nhtml = '';
		$.each( navData.navbar , function(i, n){
			nhtml += '<li id="'+n.id+'" class="openwin '+n.class+'" ehref="'+n.href+'">';
			nhtml += '<img src="'+n.icon+'"/>';
			nhtml += '</li>';
		});
		$('.navbar_top>ul').html(nhtml);
	}
	iconinit();
	
	//初始化方法
	function init(){
		var width = $(window).width();
		var height = $(window).height();
		$('body').css('width',width+'px');
		$('body').css('height',height+'px');
		//桌面设置
		$('#desktop').width(width-90);
		$('#desktop').height(height-75-35);
		$('#desktop').css('margin-bottom','75px');
		$('#desktop').css('margin-left','90px');
		$('#desktop').css('margin-top','35px');
		//所有程序设置
		$('#allpro').width(width-90-20);
		$('#allpro').height(height-75-35);
		$('#allpro').css('margin-bottom','75px');
		$('#allpro').css('margin-left','90px');
		$('#allpro').css('margin-top','35px');
		
		iconsort(); //图标排列
		
	}
	
	//初始化调用
	init();
	//窗口改变大小调用
	$(window).on('resize',function(){
		init();
	});
	
	//桌面图标排列方法
	function iconsort() {
		var direction = 'vertical'; //支持水平（level）、垂直（vertical）
		var seq_count = 5; //每序列图标个数
		var icon_width = 86; //图标宽度
		var icon_heigh = 88; //图标高度
		var icon_margin_left = 0; //图标左间距
		var icon_margin_right = 12; //图标右间距
		var icon_margin_top = 0; //图标上间距
		var icon_margin_bottom = 12; //图标下间距
		
		var icon_all_width = icon_width+icon_margin_left+icon_margin_right; //总宽度
		var icon_all_heigh = icon_heigh+icon_margin_top+icon_margin_bottom; //总高度
		
		//重新计算序列图标个数
		var wheight = $(window).height();
		var dheight = wheight-75-35;
		seq_count = Math.floor(dheight/icon_all_heigh);
		
		var seq_x = 0; //序列横轴，默认从0开始计数
		var seq_y = 0; //序列纵轴，默认从0开始计数
		$('#desktop>ul>li').each(function(index, element) {
			$(this).css('left',(icon_all_width*seq_x)+'px'); //图标X轴位置
			$(this).css('top',(icon_all_heigh*seq_y)+'px'); //图标Y轴位置
			
			//序列排满换下一序列
			if(direction=='vertical'){
				seq_y++;
				if(seq_y==seq_count){
					seq_y = 0;
					seq_x ++;
				}
			}else{
				seq_x++;
				if(seq_x==seq_count){
					seq_x = 0;
					seq_y ++;
				}
			}		
		});
	}
	
	//绑定窗口打开事件
	$('.openwin').each(function(index, element) {
		$(this).on('click',function(){
			var id = $(this).attr('id');
			var href = $(this).attr('ehref');
			var ewidth = $(this).attr('ewidth');
			var eheight = $(this).attr('eheight');
			var title = $(this).attr('etitle');
			$.dialog({
				id:id,
				title:title,
				content: 'url:'+href,
				width:ewidth,
				height:eheight,
				init: function(){
					windowManage();
				},
				close: function(){
					$('body').oneTime('100ms',function(){
						windowManage();
					});
				},
				minfun: function(){
					$('body').oneTime('100ms',function(){
						windowManage();
					});
				}
				,
				focusfun: function(){
					$('body').oneTime('100ms',function(){
						windowManage();
					});
				}
			});
		});
	});
	
	
	//任务栏事件
	$('.tasklist>li').live('click',function(){
		var id = $(this).attr('toid');
		if($.dialog.list[id]){
			//console.log($.dialog.list[id]);
			//当窗口是聚焦时，单击任务栏则最小化
			if($.dialog.list[id].getFocusStatus()){
				$.dialog.list[id].min();
			}else{
				//当窗口非聚焦时，单击任务栏则显示该窗口并聚焦
				if($.dialog.list[id].getMinStatus()){
					$.dialog.list[id].min();
				}
				$.dialog.list[id].focus();
				$.dialog.list[id].zindex();
			}			
		}
		windowManage();
	});
	
	//关闭事件
	/*$('.ui_close').on('click',function(){
		windowManage();
	});*/
	
	//窗口管理方法
	function windowManage(){
		var html = '';
		
		var list = $.dialog.list;
		for( var i in list ){
			if($.trim(list[i].config.title)=='视窗'){
				continue;
			}
			html += '<li toid="'+list[i].config.id+'">'+list[i].config.title+'</li>';
		}
		
		$('.tasklist').html(html);
		
		//当前的高亮
		if($.dialog.focus && !$.dialog.focus._minState){
			$('.tasklist>li[toid="'+$.dialog.focus.config.id+'"]').addClass('hover').siblings().removeClass('hover');
		}
	}
	
	window.windowManage = windowManage;
	
	//桌面和所有程序切换功能
	$('#cutover>li').on('click',function(){
		if($(this).attr('for')=='desktop'){
			$('#desktop').slideDown();
			$('#allpro').slideUp();
			
			var list = $.dialog.list;
			for( var i in list ){
				list[i].min();
			}
		}else{
			$('#allpro').slideDown();
			$('#desktop').slideUp();
			
			var list = $.dialog.list;
			for( var i in list ){
				list[i].min();
			}
		}
		
		$(this).addClass('current').siblings().removeClass('current');
	});
	$('.navbar_bottom').on('click',function(){
		if($('#cutover>li:eq(0)').is('.current')){
			$('#cutover>li:eq(1)').click();
		}else{
			$('#cutover>li:eq(0)').click();
		}
	});
	
	//左右滑动按钮
	function allproShow(){
		var allpro_left_width = $('#allpro').width();
		var allpro_left_height = $('#allpro').height();
		$('#allpro .allpro_left').css('margin-top',(allpro_left_height-44)/2+'px');
		$('#allpro .allpro_right').css('margin-top',(allpro_left_height-44)/2+'px');
		$('#allpro .allpro_center_bg').css('width',(allpro_left_width-112)+'px');
		var scorllleft = $("#allpro .allpro_center").position().left;	
		var alllength = $("#allpro .allpro_center ul").length*236;
		//$('#allpro .allpro_center').css('width',alllength+'px');
	
		$('#allpro .allpro_left').click(function(){//左按钮
			var scorllleft = $("#allpro .allpro_center").position().left;	
			var sleft = -(alllength - allpro_left_width);
			if(scorllleft > sleft){
				$("#allpro .allpro_center:not(:animated)").animate({left:(scorllleft-236)+"px"},"fast");
				$('#allpro .allpro_right').removeClass('allpro_right1');
			}else{
				$('#allpro .allpro_left').addClass('allpro_left1');
			}
		});
		
		if(scorllleft == 0){
			$('#allpro .allpro_right').addClass('allpro_right1');
		}else{
			$('#allpro .allpro_right').removeClass('allpro_right1');
		}
		
		$('#allpro .allpro_right').click(function(){//右按钮
			var scorllleft = $("#allpro .allpro_center").position().left;
			if(scorllleft == 0){
				$('#allpro .allpro_right').addClass('allpro_right1');
			}else{
				$('#allpro .allpro_right').removeClass('allpro_right1');
				$("#allpro .allpro_center:not(:animated)").animate({left:(scorllleft+236)+"px"},"fast");
				$('#allpro .allpro_left').removeClass('allpro_left1');
			}
		});
	}
	
	allproShow();
	$(window).on('resize',function(){
		allproShow();
	});
	
	//主导航工具按钮
	$('#fullScreen').on('click',function(){
		var e = jQuery.Event("keydown");//模拟一个键盘事件 
		e.keyCode = 122;//按F11 
		$('body').trigger(e);
		
		//console.log('aa');
	});
});